package common;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public abstract class AbstractRunner {
	
	public static enum SIZE {
		test,
		small,
		large
	}
	
	public BufferedReader in;
	public BufferedWriter out;
	public int ncases;
	public SIZE size;
	public Scanner scanner;
		
	public AbstractRunner init(SIZE size) {
		this.size = size;
		return this;
	}
	
	public abstract void handleCase(int caseNumber) throws Exception;
	
	private String makeFilename(String suffix) {
		return "gcj-src/"+getClass().getName().replace('.', '/').replace("$", "") + "-"+size+suffix;		
	}
	
	public void execute() throws Exception {
		String inFileName = makeFilename(".in");
		if (!new File(inFileName).exists()) {			
			inFileName = makeFilename("-practice.in");			
		}
		
		String outFileName = makeFilename(".out");
		
		in = new BufferedReader(new FileReader(inFileName));
		out = new BufferedWriter(new FileWriter(outFileName));
		scanner = new Scanner(in);
		ncases = scanner.nextInt();
		scanner.nextLine();
		
		for (int caseNumber=1; caseNumber <= ncases; caseNumber++) {
			out.write("Case #"+caseNumber+": ");
			handleCase(caseNumber);
		}		
		
		scanner.close();
		out.close();
	}

	public void println(double d) throws IOException {
		println(Double.toString(d));
	}
	
	public void println(long l) throws IOException {
		println(Long.toString(l));
	}
	
	public void println(String str) throws IOException {
		out.write(str+"\n");
		System.out.println(str+"\n");
	}
}
